/**
 * 
 */
package cn.gov.msa.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.UsesSunHttpServer;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;

import cn.gov.msa.collection.LdapService;
import cn.gov.msa.dao.ApplyMapper;
import cn.gov.msa.dao.AuditAnnexMapper;
import cn.gov.msa.dao.AuditOpinionMapper;
import cn.gov.msa.dao.DeclareItemMapper;
import cn.gov.msa.dao.FlowBusinessMapper;
import cn.gov.msa.dao.FlowInstanceMapper;
import cn.gov.msa.dao.FlowMapper;
import cn.gov.msa.dao.FlowNextNodeMapper;
import cn.gov.msa.dao.FlowNodeMapper;
import cn.gov.msa.dao.FlowNodeParamMapper;
import cn.gov.msa.dao.FlowTaskMapper;
import cn.gov.msa.dao.FlowTaskUserMapper;
import cn.gov.msa.dao.RecordCompanyMapper;
import cn.gov.msa.dao.UserDetailMapper;
import cn.gov.msa.model.Apply;
import cn.gov.msa.model.AuditAnnex;
import cn.gov.msa.model.AuditOpinion;
import cn.gov.msa.model.DeclareItem;
import cn.gov.msa.model.Flow;
import cn.gov.msa.model.FlowBusiness;
import cn.gov.msa.model.FlowInstance;
import cn.gov.msa.model.FlowNextNode;
import cn.gov.msa.model.FlowNextNodeE;
import cn.gov.msa.model.FlowNode;
import cn.gov.msa.model.FlowNodeParam;
import cn.gov.msa.model.FlowTask;
import cn.gov.msa.model.FlowTaskUser;
import cn.gov.msa.model.RecordCompany;
import cn.gov.msa.model.UserRole;
import cn.gov.msa.service.FlowService;
import cn.gov.msa.service.TaskService;
import cn.gov.msa.util.DateUtils;
import cn.gov.msa.util.MultiTaskService;
import cn.gov.msa.util.PropertiesTools;
import cn.gov.msa.util.WebServiceTools;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class TaskServiceImpl implements TaskService {

	@Autowired
	FlowMapper flowMapper;
	@Autowired
	FlowNodeMapper flowNodeMapper;
	@Autowired
	FlowNextNodeMapper flowNextNodeMapper;
	@Autowired
	FlowBusinessMapper flowBusinessMapper;
	@Autowired
	FlowNodeParamMapper flowNodeParamMapper;
	@Autowired 
	FlowInstanceMapper flowInstanceMapper;
	@Autowired 
	FlowTaskMapper flowTaskMapper;
	@Autowired 
	FlowTaskUserMapper flowTaskUserMapper;
	
	private static String todourl  = PropertiesTools.readProperties("todo.url");
	
	@Override
	public JSONObject gTasksDeleteByAppId(JSONObject param) throws Exception {
		if("1".equals(PropertiesTools.readProperties("project.4atodo"))) {
			return JSONObject.fromObject(WebServiceTools.getDataByUrl(todourl, "gTasksDeleteByAppId", param.toString()));
		}else {
			return JSONObject.fromObject("{'result':'1'}");
		}
	}
	@Override
	public void gTasksBatchDeleteByAppId(final JSONArray param) throws Exception {
		if("1".equals(PropertiesTools.readProperties("project.4atodo"))) {
			final MultiTaskService multiTaskService = new MultiTaskService();
			multiTaskService.executorTask(new Runnable() {
				@Override
				public void run() {
					try {
						for (int i = 0; i < param.size(); i++) {
							Thread.sleep(10000);
							System.out.println("4A任务删除"+param.getJSONObject(i)+DateUtils.getNow());
							System.out.println(WebServiceTools.getDataByUrl(todourl, "gTasksDeleteByAppId", param.getJSONObject(i).toString()));
						}
					} catch (Exception e) {
						throw new RuntimeException(e);
					} finally {
						multiTaskService.clean();
					}
				}
			});
		}
	}
	
	@Override
	public JSONObject gTasksUpdateOrAdd(JSONObject param) throws Exception {
		if("1".equals(PropertiesTools.readProperties("project.4atodo"))) {
			return JSONObject.fromObject(WebServiceTools.getDataByUrl(todourl, "gTasksUpdateOrAdd", param.toString()));
		}else {
			return JSONObject.fromObject("{'result':'1'}");
		}
	}
	@Override
	public JSONObject gTaskBatchAdd(JSONArray param) throws Exception {
		if("1".equals(PropertiesTools.readProperties("project.4atodo"))) {
			return JSONObject.fromObject(WebServiceTools.getDataByUrl(todourl, "gTaskBatchAdd", param.toString()));
		}else {
			return JSONObject.fromObject("{'result':'1'}");
		}
	}
	@Override
	public JSONObject gTasksFind(JSONObject param) throws Exception {
		if("1".equals(PropertiesTools.readProperties("project.4atodo"))) {
			return JSONObject.fromObject(WebServiceTools.getDataByUrl(todourl, "gTasksFind", param.toString()));
		}else {
			return JSONObject.fromObject("{'result':'1'}");
		}
	}
}
